package com.jack.code;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhang_j on 2019/10/21
 */
public class Executor implements Watcher, Runnable, DataMonitorListener{

    private DataMonitor dm;
    private ZooKeeper zk;
    private String[] exec;
    private Process child;

    private Executor(String hostPort, String znode, String[] exec) throws IOException {
        this.exec = exec;
        zk = new ZooKeeper(hostPort, 3000, this);
        dm = new DataMonitor(zk, znode, null, this);
    }

    public static void main(String[] args) {
        args = new String[]{"localhost:2181","/logs"};
        String hostPort = args[0];
        String znode = args[1];
        String[] exec =  new String[]{"cmd.exe","time"};

        try {
            new Executor(hostPort, znode, exec).run();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void exists(byte[] data) {
        if(data == null){
            if(child != null){
                System.out.println("Killing process");
                child.destroy();
            }
            try{
                child.waitFor();
            }catch (InterruptedException e){

            }
            child = null;
        }else{
            if(child != null){
                System.out.println("Stopping child");
                child.destroy();

                try{
                    child.waitFor();
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
            System.out.println("===SHOW DATA===");
            System.out.println(new String(data));
            try{
                System.out.println("Starting child");
                child = Runtime.getRuntime().exec(exec);
//                List<String> list = new ArrayList<String>();
//                list.add("cmd.exe");
//                list.add("time");
//                child = new ProcessBuilder().command(list).start();
                new StreamWriter(child.getInputStream(),System.out);
                new StreamWriter(child.getErrorStream(), System.err);
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    static class StreamWriter extends Thread{
        OutputStream os;
        InputStream is;

        StreamWriter(InputStream is, OutputStream os) {
            this.is = is;
            this.os = os;
            start();
        }

        @Override
        public void run(){
            byte[] b = new byte[80];
            int rc;
            try {
                System.out.println("===========START TO WRITE===========");
                while ((rc = is.read(b)) > 0) {
                    os.write(b, 0, rc);
                }
                System.out.println("===========STOP TO WRITE===========");
            } catch (IOException e) {
            }
        }
    }

    public void closing(int rc) {
        synchronized (this) {
            System.out.println("===========EXECUTOR START TO NOTIFY ALL===========");
            notifyAll();
            System.out.println("===========EXECUTOR START TO NOTIFY ALL===========");
        }
    }

    public void run() {
        try {
            synchronized (this) {
                while (!dm.dead) {
                    System.out.println("===========EXECUTOR START TO WAIT===========");
                    wait();
                    System.out.println("===========EXECUTOR STOP WAIT===========");
                }
            }
        } catch (InterruptedException e) {
        }
    }

    public void process(WatchedEvent event) {
        dm.process(event);
    }
}
